from rest_framework import serializers
from main.script.models import Script, ScriptRecord
from main.script.models import ScriptGroup
from utils.serializers import CustomModelSerializer


class ScriptCreateUpdateSerializer(CustomModelSerializer):
    """
    脚本创建更新 序列化器
    """
    parameters = serializers.JSONField()

    class Meta:
        model = Script
        fields = '__all__'

    def create(self, validated_data):
        validated_data['parameters'] = Script.make_parameters(validated_data['parameters'])
        return super().create(validated_data)

    def update(self, instance, validated_data):
        validated_data['parameters'] = Script.make_parameters(validated_data['parameters'])
        return super().update(instance, validated_data)


class ScriptSerializer(CustomModelSerializer):
    """
    脚本管理-序列化器
    """

    parameters = serializers.SerializerMethodField()
    group_name = serializers.CharField(source='group.name')
    hostname = serializers.CharField(source='host.hostname')
    build_at = serializers.SerializerMethodField()
    build_id = serializers.SerializerMethodField()
    build_time = serializers.SerializerMethodField()
    build_state = serializers.SerializerMethodField()
    build_token = serializers.SerializerMethodField()

    class Meta:
        model = Script
        fields = '__all__'
        read_only_fields = ['id']

    def get_build(self, obj):
        build = obj.scriptrecord_set.order_by('-create_datetime').first()
        return build

    def get_build_id(self, obj):
        building = obj.scriptrecord_set.filter(state=2)
        if building:
            return building.order_by('-id').first().build_id
        return ''

    def get_build_at(self, obj):
        if self.get_build(obj):
            return self.get_build(obj).build_at

    def get_build_state(self, obj):
        if self.get_build(obj):
            return self.get_build(obj).state

    def get_build_token(self, obj):
        if self.get_build(obj):
            return self.get_build(obj).token

    def get_build_time(self, obj):
        if self.get_build(obj):
            return self.get_build(obj).build_time

    def get_parameters(self, obj):
        return eval(obj.parameters)


class ScriptDetailSerializer(CustomModelSerializer):
    """
    脚本记录-序列化器
    """

    class Meta:
        model = ScriptRecord
        fields = '__all__'
        read_only_fields = ['id']


class ScriptGroupSerializer(CustomModelSerializer):
    """
    主机组管理 简单序列化器
    """
    key = serializers.CharField(source='id')
    value = serializers.CharField(source='id')
    title = serializers.CharField(source='name')
    children = serializers.SerializerMethodField()
    pid = serializers.SerializerMethodField()

    class Meta:
        model = ScriptGroup
        fields = ('id', 'key', 'value', 'title', 'children', 'pid')

    def get_children(self, obj):
        if obj.self_parent:
            return ScriptGroupSerializer(obj.self_parent.order_by('sort_id'), many=True).data
        return None

    def get_pid(self, obj):
        return obj.parent.id if obj.parent else 0


class ScriptGroupCreateUpdateSerializer(CustomModelSerializer):
    """
    脚本组管理 创建/更新时的列化器
    """

    def create(self, validated_data):
        group = ScriptGroup.objects.all().order_by('-sort_id').first()
        if group:
            validated_data['sort_id'] = group.sort_id + 1
        return super().create(validated_data)

    class Meta:
        model = ScriptGroup
        fields = '__all__'
